package com.whut.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whut.mapper.*;
import com.whut.model.*;
import com.whut.service.FuzzyService;
import com.whut.utils.StringOperate;
import com.whut.utils.pointsCalculate;
import com.whut.v_model.ProblemCollectDetail;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.*;
@Slf4j
@Service("fuzzyService")
public class FuzzyServiceImpl implements FuzzyService {
    @Resource
    private SpecialistinfoMapper specialistinfoMapper;
    @Resource
    private JudgeinfoMapper judgeinfoMapper;
    @Resource
    private NewPrefixSystemMapper newprefixsystemMapper;
    @Resource
    private MembershipinfoMapper membershipinfoMapper;
    @Resource
    private ProblemCollectDetailMapper problemCollectDetailMapper;
    @Resource
    private FuzzyindicatorsMapper fuzzyindicatorsMapper;
    @Resource
    private FuzzyresultMapper fuzzyresultMapper;
    @Resource
    private ContracttaskprojectMapper contracttaskprojectMapper;
    @Resource
    private ProblemcollectMapper problemcollectMapper;

    @Override
    public Object getAllSpecialistByPage(int curPage, int pageSize) {
        PageHelper.startPage(curPage, pageSize);
        List<Specialistinfo> list = specialistinfoMapper.getAllSpecialistInfo();
        PageInfo result = new PageInfo(list);
        return result;
    }

    @Override
    public Object getAllJudgeInfo(int specialistId) {
        List<Object> list = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        List<Object> list1 = new ArrayList<>();
        List<Object> list2 = new ArrayList<>();
        List<Integer> allItemsList = judgeinfoMapper.getAllId(specialistId);
        for (int i = 0; i < allItemsList.size() + 1; i++) {
            Map<String, Object> map1 = new HashMap<>();
            if (i == 0) {
                map1.put("id", "name");
                map1.put("value", "检查项");
            } else {
                map1.put("id", allItemsList.get(i - 1).toString());
                map1.put("value", newprefixsystemMapper.getName4ByName4Id(allItemsList.get(i - 1)));
            }
            if (i < allItemsList.size()) {
                Map<String, Object> map2 = new HashMap<>();
                map2.put("name", newprefixsystemMapper.getName4ByName4Id(allItemsList.get(i)));
                map2.put("id", allItemsList.get(i));
                for (int j = 0; j < allItemsList.size(); j++) {
                    map2.put(allItemsList.get(j).toString(), judgeinfoMapper.getPointById1AndId2(specialistId, allItemsList.get(j), allItemsList.get(i)));
                }
                list2.add(map2);
            }
            list1.add(map1);
        }
        map.put("list1", list1);
        map.put("list2", list2);
        list.add(map);
        return list;
    }

    @Override
    public String editJudgeInfo(int id1, int id2, double point, int specialistId, int id3) {
        try {
            judgeinfoMapper.updatePointById1AndId2AndSpecialistId(id1, id2, point, specialistId,id3);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }

    @Override
    public Object getChooseInfoForJudge() {
        List<Object> list = new ArrayList<Object>();
        List<Object> pNname1InfoList = newprefixsystemMapper.getNumberForJudge("1", 0);
        String pName1Info = JSON.toJSONString(pNname1InfoList, SerializerFeature.WriteMapNullValue, SerializerFeature.DisableCircularReferenceDetect);
        JSONArray pNname1InfoArray = JSONArray.parseArray(pName1Info);
        for (int i = 0; i < pNname1InfoArray.size(); i++) {
            List<Object> listName1 = new ArrayList<Object>();
            Map<String, Object> name1Map = new HashMap<String, Object>();
            int firstId = pNname1InfoArray.getIntValue(i);
            String pName1 = newprefixsystemMapper.getName4ByName4Id(firstId);
            List<Object> secondIdInfoList = newprefixsystemMapper.getNumberForJudge("2", firstId);
            String secondIdInfo = JSON.toJSONString(secondIdInfoList, SerializerFeature.WriteMapNullValue, SerializerFeature.DisableCircularReferenceDetect);
            JSONArray secondIdInfoArray = JSONArray.parseArray(secondIdInfo);
            for (int j = 0; j < secondIdInfoArray.size(); j++) {
                List<Object> listName2 = new ArrayList<Object>();
                Map<String, Object> name2Map = new HashMap<String, Object>();
                int secondId = secondIdInfoArray.getIntValue(j);
                List<Object> thirdIdInfoList = newprefixsystemMapper.getNumberForJudge("3", secondId);
                System.out.println("我没有办法，为了去除重复标记");
                String thirdIdInfo = JSON.toJSONString(thirdIdInfoList, SerializerFeature.WriteMapNullValue, SerializerFeature.DisableCircularReferenceDetect);
                JSONArray thirdIdInfoArray = JSONArray.parseArray(thirdIdInfo);
                for (int k = 0; k < thirdIdInfoArray.size(); k++) {
                    List<Object> listName3 = new ArrayList<Object>();
                    Map<String, Object> name3Map = new HashMap<String, Object>();
                    System.out.println("一层嵌套一层");
                    int thirdId = thirdIdInfoArray.getIntValue(k);
                    List<Object> fourthIdInfoList = newprefixsystemMapper.getNumberForJudge("4", thirdId);
                    String fourthIdInfo = JSON.toJSONString(fourthIdInfoList, SerializerFeature.WriteMapNullValue, SerializerFeature.DisableCircularReferenceDetect);
                    JSONArray fourthIdInfoArray = JSONArray.parseArray(fourthIdInfo);
                    for (int l = 0; l < fourthIdInfoArray.size(); l++) {
                        System.out.println("唉");
                        Map<String, Object> name4Map = new HashMap<String, Object>();
                        int fourthId = fourthIdInfoArray.getIntValue(l);
                        String pName4 = newprefixsystemMapper.getName4ByName4Id(fourthId);
                        name4Map.put("id", fourthId);
                        name4Map.put("name", pName4 + "(" + fourthId + ")");
                        listName3.add(name4Map);
                    }
                    String pName3 = newprefixsystemMapper.getName4ByName4Id(thirdId);
                    name3Map.put("id", thirdId);
                    name3Map.put("name", pName3);
                    name3Map.put("children", listName3);
                    listName2.add(name3Map);
                }
                String pName2 = newprefixsystemMapper.getName4ByName4Id(secondId);
                name2Map.put("id", secondId);
                name2Map.put("name", pName2);
                name2Map.put("children", listName2);
                System.out.println("我也很难受");
                listName1.add(name2Map);
            }
            name1Map.put("id", firstId);
            name1Map.put("name", pName1);
            name1Map.put("children", listName1);
            list.add(name1Map);
        }
        return list;
    }

    @Override
    public String addJudgeItems(int specialistId, int fourthId, int id3) {
        List<Integer> allItemsList = judgeinfoMapper.getAllId1(specialistId,id3);
        Judgeinfo judgeinfo = new Judgeinfo();
        judgeinfo.setId1(fourthId);
        judgeinfo.setId2(fourthId);
        judgeinfo.setId3(id3);
        judgeinfo.setSpecialistId(specialistId);
        judgeinfo.setPoint(0.0);
        judgeinfoMapper.insertSelective(judgeinfo);
        for (int i = 0; i < allItemsList.size(); i++) {
            Judgeinfo judgeinfo1 = new Judgeinfo();
            judgeinfo1.setId1(fourthId);
            judgeinfo1.setId2(allItemsList.get(i));
            judgeinfo1.setId3(id3);
            judgeinfo1.setSpecialistId(specialistId);
            judgeinfo1.setPoint(0.0);
            judgeinfoMapper.insertSelective(judgeinfo1);
            Judgeinfo judgeinfo2 = new Judgeinfo();
            judgeinfo2.setId2(fourthId);
            judgeinfo2.setId3(id3);
            judgeinfo2.setId1(allItemsList.get(i));
            judgeinfo2.setSpecialistId(specialistId);
            judgeinfo2.setPoint(0.0);
            judgeinfoMapper.insertSelective(judgeinfo2);
        }
        return "success";
    }

    @Override
    public Float consistCheck(int specialistId, int id3) {
        List<Integer> allItemsList = judgeinfoMapper.getAllId1(specialistId,id3);
        int itemsNum = allItemsList.size();
        float ri = 0;
        switch (itemsNum) {
            case 1:
                ri = 0;
                break;
            case 2:
                ri = 0;
                break;
            case 3:
                ri = 0.58f;
                break;
            case 4:
                ri = 0.90f;
                break;
            case 5:
                ri = 1.12f;
                break;
            case 6:
                ri = 1.24f;
                break;
            case 7:
                ri = 1.32f;
                break;
            case 8:
                ri = 1.41f;
                break;
            case 9:
                ri = 1.45f;
                break;
            case 10:
                ri = 1.49f;
                break;
            case 11:
                ri = 1.52f;
                break;
            case 12:
                ri = 1.54f;
                break;
            case 13:
                ri = 1.56f;
                break;
            case 14:
                ri = 1.58f;
                break;
            case 15:
                ri = 1.59f;
                break;
            case 16:
                ri = 1.5943f;
                break;
            case 17:
                ri = 1.6064f;
                break;
            case 18:
                ri = 1.6133f;
                break;
            case 19:
                ri = 1.6207f;
                break;
            case 20:
                ri = 1.6292f;
                break;
            case 21:
                ri = 1.6385f;
                break;
            case 22:
                ri = 1.6403f;
                break;
            case 23:
                ri = 1.6462f;
                break;
            case 24:
                ri = 1.6497f;
                break;
            case 25:
                ri = 1.6556f;
                break;
            case 26:
                ri = 1.6587f;
                break;
            case 27:
                ri = 1.6631f;
                break;
            case 28:
                ri = 1.667f;
                break;
            case 29:
                ri = 1.6693f;
                break;
            case 30:
                ri = 1.6724f;
                break;
            default:
        }
        List<Float> weightList = new ArrayList<>();
        for (int i = 0; i < itemsNum; i++) {
            List<Float> everyLineList = new ArrayList<>();
            for (int j = 0; j < allItemsList.size(); j++) {
                everyLineList.add(judgeinfoMapper.getEveryLine1(allItemsList.get(j), allItemsList.get(i), specialistId,id3));
            }
            System.out.println(i + "hehehdasdasdadadsadasd" + StringOperate.multiply(everyLineList));
            weightList.add(StringOperate.multiply(everyLineList));
        }
        List<Float> wiList = StringOperate.normalization(weightList);
        float allAwi = 0;
        for (int i = 0; i < itemsNum; i++) {
            float innerAllAwi = 0;
            List<Float> everyLineList = new ArrayList<>();
            for (int k = 0; k < allItemsList.size(); k++) {
                everyLineList.add(judgeinfoMapper.getEveryLine1(allItemsList.get(k), allItemsList.get(i), specialistId,id3));
            }
            String aaa = JSON.toJSONString(everyLineList, SerializerFeature.WriteMapNullValue, SerializerFeature.DisableCircularReferenceDetect);
            System.out.println(aaa);
            for (int j = 0; j < itemsNum; j++) {
                innerAllAwi += everyLineList.get(j) * wiList.get(j);
            }
            System.out.println("权重" + i + ":" + wiList.get(i));
            allAwi += innerAllAwi / wiList.get(i);
            System.out.println("awi:" + i + "asdasdasdasd" + innerAllAwi);
            System.out.println("allawi:" + i + "asdasdasdasd" + allAwi);
        }
        float result = (allAwi / itemsNum - itemsNum) / (itemsNum - 1) / ri;
        return result;
    }

    @Override
    public List<Membershipinfo> getAllMembership(Integer specialistId) {
        return membershipinfoMapper.getAllMembershipById(specialistId);

    }

    @Override
    public String updateZj(Specialistinfo zj) {
        specialistinfoMapper.updateByPrimaryKeySelective(zj);
        return "success";
    }

    @Override
    public void editMemberShipInfo(Membershipinfo membershipinfo) {
        //membershipinfoMapper.editMemberShipInfo(membershipinfo);
        membershipinfoMapper.updateByPrimaryKey(membershipinfo);
    }

    @Override
    public void deleteMemberShipInfo(Integer mkey) {
        membershipinfoMapper.deleteByPrimaryKey(mkey);
    }

    @Override
    public void addMemberShipInfo(Membershipinfo membershipinfo) {
        membershipinfoMapper.addMemberShipInfo(membershipinfo);
    }

    @Override
    public void importFileData(List<Membershipinfo> list) {
        list.forEach((m) -> {
            membershipinfoMapper.importFileData(m);
        });
    }

    @Override
    public void addJudgeInfo(Judgeinfo judgeinfo) {
        judgeinfoMapper.addJudgeInfo(judgeinfo);
    }

    @Override
    public void deleteBySpecialistId(Integer specialistId, Integer id3) {
        judgeinfoMapper.deleteBySpecialistId(specialistId,id3);
    }

    @Override
    public void deleteMemberShipInfoBySpecialistId(Integer specialistId) {
        membershipinfoMapper.deleteMemberShipInfoBySpecialistId(specialistId);
    }

    @Override
    public Object getAllJudgeResult(int checkId) {
        int countFuzzyIndicators = fuzzyindicatorsMapper.getCountByCheckId(checkId);
        List<Object> list = new ArrayList<>();
        List<Object> list1 = new ArrayList<>();
        List<Object> list2 = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        List<Integer> allItemsList = problemCollectDetailMapper.getAllFourthIdByNum(checkId, 1);
        List<Specialistinfo> specialistList = specialistinfoMapper.getAllSpecialistInfo();
        for (int i = 0; i < allItemsList.size() + 1; i++) {
            Map<String, Object> map1 = new HashMap<>();
            if (i == 0) {
                map1.put("id", "name");
                map1.put("value", "专家姓名");
            } else {
                map1.put("id", allItemsList.get(i - 1).toString());
                map1.put("value", newprefixsystemMapper.getName4ByName4Id(allItemsList.get(i - 1)));
            }
            list1.add(map1);
        }
        List<Float> allWeightList = new ArrayList<>();
        for (int i = 0; i < specialistList.size(); i++) {
            List<Float> weightList = new ArrayList<>();
            for (int k = 0; k < allItemsList.size(); k++) {
                List<Float> everyLineList = new ArrayList<>();
                for (int m = 0; m < allItemsList.size(); m++) {
                    everyLineList.add(judgeinfoMapper.getEveryLine(allItemsList.get(m), allItemsList.get(k), specialistList.get(i).getMkey()));
                }
                weightList.add(StringOperate.multiply(everyLineList));
            }
            List<Float> wiList = StringOperate.normalization(weightList);
            Map<String, Object> map2 = new HashMap<>();
            map2.put("name", specialistList.get(i).getName());
            for (int j = 0; j < allItemsList.size(); j++) {
                map2.put(allItemsList.get(j).toString(), wiList.get(j));
                Float judgeWeight = (float) pointsCalculate.getPercentInstance(specialistList.get(i).getJudgeWeight());
                allWeightList.add(wiList.get(j) * judgeWeight);
            }
            list2.add(map2);
        }
        Map<String, Object> map2 = new HashMap<>();
        List<Float> list3 = new ArrayList<>();
        map2.put("name", "总权重");
        for (int i = 0; i < allItemsList.size(); i++) {
            Float weight = 0f;
            for (int j = i; j < allWeightList.size(); j += allItemsList.size()) {
                weight += allWeightList.get(j);
            }
            map2.put(allItemsList.get(i).toString(), weight);
            list3.add(weight);
        }
        if (countFuzzyIndicators == 0) {
            Fuzzyindicators fuzzyindicators = new Fuzzyindicators();
            fuzzyindicators.setCheckId(checkId);
            fuzzyindicators.setNumber(-1);
            fuzzyindicators.setIndicatorsLevel(-1);
            fuzzyindicators.setIndicatorsName("安全实体");
            fuzzyindicators.setIndicatorsWeight("未设定");
            fuzzyindicatorsMapper.insert(fuzzyindicators);
        }
        List<Integer> insertList = problemCollectDetailMapper.getAllFourthId(checkId);
        for (int i = 0; i < insertList.size(); i++) {
            if (newprefixsystemMapper.getFather(newprefixsystemMapper.getFather(newprefixsystemMapper.getFather(insertList.get(i)).getPreNum()).getPreNum()).getPreNum() == 1) {
                System.out.println("窝窝头，一块钱四个，嘿嘿");
            } else {
                if (countFuzzyIndicators == 0) {
                    int countThirdIdInIndicators = fuzzyindicatorsMapper.getCountByNum(checkId, newprefixsystemMapper.getFather(insertList.get(i)).getPreNum());
                    if (countThirdIdInIndicators == 0) {
                        Fuzzyindicators fuzzyindicators = new Fuzzyindicators();
                        fuzzyindicators.setCheckId(checkId);
                        fuzzyindicators.setIndicatorsLevel(1);
                        fuzzyindicators.setNumber(newprefixsystemMapper.getFather(insertList.get(i)).getPreNum());
                        fuzzyindicators.setIndicatorsName(newprefixsystemMapper.getName4ByName4Id(newprefixsystemMapper.getFather(insertList.get(i)).getPreNum()) + "管理行为");
                        fuzzyindicators.setIndicatorsWeight("未设定");
                        fuzzyindicatorsMapper.insert(fuzzyindicators);
                    } else {
                        System.out.println("窝窝头，四块钱一个，嘿嘿");
                    }
                }
                if (countFuzzyIndicators == 0) {
                    Fuzzyindicators fuzzyindicators = new Fuzzyindicators();
                    fuzzyindicators.setCheckId(checkId);
                    fuzzyindicators.setNumber(insertList.get(i));
                    fuzzyindicators.setIndicatorsLevel(2);
                    fuzzyindicators.setIndicatorsName(newprefixsystemMapper.getName4ByName4Id(insertList.get(i)));
                    fuzzyindicators.setIndicatorsWeight("未设定");
                    fuzzyindicatorsMapper.insert(fuzzyindicators);
                }
            }
        }
        list2.add(map2);
        List<Fuzzyindicators> list4 = fuzzyindicatorsMapper.selectAllByCheckId(checkId);
        map.put("list1", list1);
        map.put("list2", list2);
        map.put("list3", list3);
        map.put("list4", list4);
        list.add(map);
        return list;
    }

    @Override
    public Object getAllFuzzyResult(String weightInfo, int checkId) {
        DecimalFormat df = new DecimalFormat("0.000");
        //看检查的第二级id是否唯一
        List<Integer> allSecondId = problemcollectMapper.getAllSecondIdByCheck(checkId,1);
        if(allSecondId.size()>1){return "error";}
        int id3 = allSecondId.get(0);
        List<Specialistinfo> specialistList = specialistinfoMapper.getAllSpecialistInfoById3(id3);
//        List<Specialistinfo> specialistList = specialistinfoMapper.getAllSpecialistInfo();
        JSONArray jsonArray = JSONArray.parseArray(weightInfo);
        List<Object> list = new ArrayList<>();
        List<Integer> allItemsList = problemCollectDetailMapper.getAllFourthId(checkId);
        int count = 0;
        //不要乱动
        int det=0;
        for (int i = 0; i < allItemsList.size(); i++) {
            List<String> allRiskPointList = problemCollectDetailMapper.allRiskPointListNotTiXing(checkId, allItemsList.get(i));
            int firstId = newprefixsystemMapper.getFather(newprefixsystemMapper.getFather(newprefixsystemMapper.getFather(allItemsList.get(i)).getPreNum()).getPreNum()).getPreNum();
            if( firstId!= 1){
                det++;
            }
            for (int j = 0; j < allRiskPointList.size(); j++) {
                Map<String, Object> map = new HashMap<>();
                // log.info("number:{},j:{}",newprefixsystemMapper.getFather(newprefixsystemMapper.getFather(newprefixsystemMapper.getFather(allItemsList.get(i)).getPreNum()).getPreNum()).getPreNum(),j);
                if (firstId == 1) {
                    count = count - j;
                    // log.info("count:{}",count);
                    map.put("firstLevelIndicators", "安全实体");
                    map.put("secondWeight", df.format(jsonArray.getFloatValue(i-det)));
                    map.put("firstLevelWeight", fuzzyindicatorsMapper.getFuzzyInfoByCheckIdAndNumber(checkId, -1).getIndicatorsWeight());
                    count++;
                } else {
                    map.put("firstLevelIndicators", newprefixsystemMapper.getName4ByName4Id(newprefixsystemMapper.getFather(allItemsList.get(i)).getPreNum()));
                    map.put("secondWeight", fuzzyindicatorsMapper.getFuzzyInfoByCheckIdAndNumber(checkId, allItemsList.get(i)).getIndicatorsWeight());
                    map.put("firstLevelWeight", fuzzyindicatorsMapper.getFuzzyInfoByCheckIdAndNumber(checkId, newprefixsystemMapper.getFather(allItemsList.get(i)).getPreNum()).getIndicatorsWeight());
                }
                map.put("secondLevelIndicators", newprefixsystemMapper.getName4ByName4Id(allItemsList.get(i)));
                List<ProblemCollectDetail> innerList = problemCollectDetailMapper.getAllFourthInfo(checkId, allItemsList.get(i));
                String maxRiskPoint = pointsCalculate.getMaxRiskPoint(innerList);
                int maxProblemNum = problemCollectDetailMapper.getProblemNumByRiskPoint(checkId, allItemsList.get(i), maxRiskPoint);
                map.put("riskPoint", allRiskPointList.get(j));
                map.put("problemNum", problemCollectDetailMapper.getProblemNumByRiskPoint(checkId, allItemsList.get(i), allRiskPointList.get(j)));
                List<Double> memberShipList = new ArrayList<>();
                for (int m = 0; m < specialistList.size(); m++) {
                    Membershipinfo membershipinfo = membershipinfoMapper.getMemberShipInfoByAllInfo(specialistList.get(m).getMkey(), maxProblemNum > 10 ? 10 : maxProblemNum, maxRiskPoint);
                    double risk1 = 0;
                    double risk2 = 0;
                    double risk3 = 0;
                    double risk4 = 0;
                    double risk5 = 0;
                    for (int y = 0; y < allRiskPointList.size(); y++) {
                        risk1 += membershipinfoMapper.getMemberShipInfoByAllInfo(specialistList.get(m).getMkey(), problemCollectDetailMapper.getProblemNumByRiskPoint(checkId, allItemsList.get(i), allRiskPointList.get(y)) > 10 ? 10 : problemCollectDetailMapper.getProblemNumByRiskPoint(checkId, allItemsList.get(i), allRiskPointList.get(y)), allRiskPointList.get(y)).getRisk1Point();
                        risk2 += membershipinfoMapper.getMemberShipInfoByAllInfo(specialistList.get(m).getMkey(), problemCollectDetailMapper.getProblemNumByRiskPoint(checkId, allItemsList.get(i), allRiskPointList.get(y)) > 10 ? 10 : problemCollectDetailMapper.getProblemNumByRiskPoint(checkId, allItemsList.get(i), allRiskPointList.get(y)), allRiskPointList.get(y)).getRisk2Point();
                        risk3 += membershipinfoMapper.getMemberShipInfoByAllInfo(specialistList.get(m).getMkey(), problemCollectDetailMapper.getProblemNumByRiskPoint(checkId, allItemsList.get(i), allRiskPointList.get(y)) > 10 ? 10 : problemCollectDetailMapper.getProblemNumByRiskPoint(checkId, allItemsList.get(i), allRiskPointList.get(y)), allRiskPointList.get(y)).getRisk3Point();
                        risk4 += membershipinfoMapper.getMemberShipInfoByAllInfo(specialistList.get(m).getMkey(), problemCollectDetailMapper.getProblemNumByRiskPoint(checkId, allItemsList.get(i), allRiskPointList.get(y)) > 10 ? 10 : problemCollectDetailMapper.getProblemNumByRiskPoint(checkId, allItemsList.get(i), allRiskPointList.get(y)), allRiskPointList.get(y)).getRisk4Point();
                        risk5 += membershipinfoMapper.getMemberShipInfoByAllInfo(specialistList.get(m).getMkey(), problemCollectDetailMapper.getProblemNumByRiskPoint(checkId, allItemsList.get(i), allRiskPointList.get(y)) > 10 ? 10 : problemCollectDetailMapper.getProblemNumByRiskPoint(checkId, allItemsList.get(i), allRiskPointList.get(y)), allRiskPointList.get(y)).getRisk5Point();
                    }
                    if (membershipinfo.getRisk5Point() != 0) {
                        membershipinfo.setRisk5Point(risk5);
                    } else if (membershipinfo.getRisk5Point() == 0 && membershipinfo.getRisk4Point() != 0) {
                        membershipinfo.setRisk4Point(risk4);
                    } else if (membershipinfo.getRisk5Point() == 0 && membershipinfo.getRisk4Point() == 0 && membershipinfo.getRisk3Point() != 0) {
                        membershipinfo.setRisk3Point(risk3);
                    } else if (membershipinfo.getRisk5Point() == 0 && membershipinfo.getRisk4Point() == 0 && membershipinfo.getRisk3Point() == 0 && membershipinfo.getRisk2Point() != 0) {
                        membershipinfo.setRisk2Point(risk2);
                    } else if (membershipinfo.getRisk5Point() == 0 && membershipinfo.getRisk4Point() == 0 && membershipinfo.getRisk3Point() == 0 && membershipinfo.getRisk2Point() == 0 && membershipinfo.getRisk1Point() != 0) {
                        membershipinfo.setRisk1Point(risk1);
                    }
                    List<Double> unNormalList = new ArrayList<>();
                    unNormalList.add(membershipinfo.getRisk1Point());
                    unNormalList.add(membershipinfo.getRisk2Point());
                    unNormalList.add(membershipinfo.getRisk3Point());
                    unNormalList.add(membershipinfo.getRisk4Point());
                    unNormalList.add(membershipinfo.getRisk5Point());
                    List<Double> normalList = StringOperate.normalization2(unNormalList);
                    if (memberShipList.size() == 0) {
                        memberShipList.add(normalList.get(0) * pointsCalculate.getPercentInstance(specialistList.get(m).getMemberShipWeight()));
                        memberShipList.add(normalList.get(1) * pointsCalculate.getPercentInstance(specialistList.get(m).getMemberShipWeight()));
                        memberShipList.add(normalList.get(2) * pointsCalculate.getPercentInstance(specialistList.get(m).getMemberShipWeight()));
                        memberShipList.add(normalList.get(3) * pointsCalculate.getPercentInstance(specialistList.get(m).getMemberShipWeight()));
                        memberShipList.add(normalList.get(4) * pointsCalculate.getPercentInstance(specialistList.get(m).getMemberShipWeight()));
                    } else {
                        memberShipList.set(0, memberShipList.get(0) + normalList.get(0) * pointsCalculate.getPercentInstance(specialistList.get(m).getMemberShipWeight()));
                        memberShipList.set(1, memberShipList.get(1) + normalList.get(1) * pointsCalculate.getPercentInstance(specialistList.get(m).getMemberShipWeight()));
                        memberShipList.set(2, memberShipList.get(2) + normalList.get(2) * pointsCalculate.getPercentInstance(specialistList.get(m).getMemberShipWeight()));
                        memberShipList.set(3, memberShipList.get(3) + normalList.get(3) * pointsCalculate.getPercentInstance(specialistList.get(m).getMemberShipWeight()));
                        memberShipList.set(4, memberShipList.get(4) + normalList.get(4) * pointsCalculate.getPercentInstance(specialistList.get(m).getMemberShipWeight()));
                    }
                }
//                提前修正
                double sum = (double)Math.round(memberShipList.get(0)*1000)/1000+(double)Math.round(memberShipList.get(1)*1000)/1000+
                        (double)Math.round(memberShipList.get(2)*1000)/1000+(double)Math.round(memberShipList.get(3)*1000)/1000+(double)Math.round(memberShipList.get(4)*1000)/1000;
                double deta= 1.000 - sum;
                if(deta!=0.000){
//                    总和小于1，将多余的修正到最大风险值
                    if(deta>0.000){
                        for(int index=4;index>=0;index--){
                            if((double)Math.round(memberShipList.get(index)*1000)/1000!=0.000){
                                memberShipList.set(index,(double)Math.round(memberShipList.get(index)*1000)/1000+deta);
                                break;
                            }
                        }
                    }else{
//                        总和大于1，将剩余的修正到最小风险值
                        for (int index=0;index<5;index++){
                            if((double)Math.round(memberShipList.get(index)*1000)/1000!=0.000){
                                memberShipList.set(index,(double)Math.round(memberShipList.get(index)*1000)/1000+deta);
                                break;
                            }
                        }
                    }
                    System.out.println(deta);
                }

                log.info("0:{} 1:{} 2:{} 3:{} 4:{} sum:{} ",(double)Math.round(memberShipList.get(0)*1000)/1000,
                        (double)Math.round(memberShipList.get(1)*1000)/1000,
                        (double)Math.round(memberShipList.get(2)*1000)/1000,
                        (double)Math.round(memberShipList.get(3)*1000)/1000,
                        (double)Math.round(memberShipList.get(4)*1000)/1000,
                        sum);
                map.put("risk0", df.format(memberShipList.get(0)));
                map.put("risk1", df.format(memberShipList.get(1)));
                map.put("risk2", df.format(memberShipList.get(2)));
                map.put("risk3", df.format(memberShipList.get(3)));
                map.put("risk4", df.format(memberShipList.get(4)));
                if(map.get("firstLevelIndicators").equals("安全实体"))
                {
                    int countAQFuzzyResult=fuzzyresultMapper.getFuzzyResultCountByCheckIdAndResultTypeAndFirstIdAndSecondId(checkId,4,1,allItemsList.get(i));
                    if(countAQFuzzyResult==0)
                    {
                       Fuzzyresult fuzzyresult=new Fuzzyresult();
                       fuzzyresult.setCheckId(checkId);
                       fuzzyresult.setResultType(4);
                       fuzzyresult.setFirstId(1);
                       fuzzyresult.setSecondId(allItemsList.get(i));
                       fuzzyresult.setRisk1Point(Double.parseDouble(df.format(memberShipList.get(0))));
                        fuzzyresult.setRisk2Point(Double.parseDouble(df.format(memberShipList.get(1))));
                        fuzzyresult.setRisk3Point(Double.parseDouble(df.format(memberShipList.get(2))));
                        fuzzyresult.setRisk4Point(Double.parseDouble(df.format(memberShipList.get(3))));
                        fuzzyresult.setRisk5Point(Double.parseDouble(df.format(memberShipList.get(4))));
                        fuzzyresultMapper.insert(fuzzyresult);
                    }else {
                        Fuzzyresult fuzzyresult=new Fuzzyresult();
                        fuzzyresult.setmKey(fuzzyresultMapper.getMkeyByCheckIdAndResultTypeAndFirstIdAndSecondId(checkId,4,1,allItemsList.get(i)));
                        fuzzyresult.setRisk1Point(Double.parseDouble(df.format(memberShipList.get(0))));
                        fuzzyresult.setRisk2Point(Double.parseDouble(df.format(memberShipList.get(1))));
                        fuzzyresult.setRisk3Point(Double.parseDouble(df.format(memberShipList.get(2))));
                        fuzzyresult.setRisk4Point(Double.parseDouble(df.format(memberShipList.get(3))));
                        fuzzyresult.setRisk5Point(Double.parseDouble(df.format(memberShipList.get(4))));
                        fuzzyresultMapper.updateByPrimaryKeySelective(fuzzyresult);
                    }
                }
                list.add(map);
            }
        }
        return list;
    }

    @Override
    public String updateFuzzyWeight(Fuzzyindicators fuzzyindicators) {
        try {
            fuzzyindicatorsMapper.updateByPrimaryKeySelective(fuzzyindicators);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }

    @Override
    public Object getFuzzyConclution(String param, int checkId, String weightInfo) {
        DecimalFormat df = new DecimalFormat("0.000");
        int fuzzyResultCount = fuzzyresultMapper.getFuzzyResultCountByCheckId(checkId);
        List<Object> allResultList = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        List<String> allAQResultList = new ArrayList<>();
        List<String> allOtherResultList = new ArrayList<>();
        List<String> allOverResultList = new ArrayList<>();
        List<String> list = fuzzyindicatorsMapper.getAllName(checkId);
        JSONArray aqWeightArray = JSONArray.parseArray(weightInfo);
        System.out.println();
        List<Double> firstWeightList = new ArrayList<>();
        List<Double> aqList = new ArrayList<>();
        JSONArray jsonArray = JSONArray.parseArray(param);
        List<Double> resultAQList = new ArrayList<>();
        List<Double> normalAQResultList = new ArrayList<>();
        List<Double> risk1AQList = new ArrayList<>();
        List<Double> risk2AQList = new ArrayList<>();
        List<Double> risk3AQList = new ArrayList<>();
        List<Double> risk4AQList = new ArrayList<>();
        List<Double> risk0AQList = new ArrayList<>();
        for (int j = 0; j < jsonArray.size(); j++) {
            JSONObject jsonObjectaq = JSONObject.parseObject(jsonArray.getString(j));
            if (jsonObjectaq.getString("firstLevelIndicators").equals("安全实体")) {
                risk0AQList.add(jsonObjectaq.getDoubleValue("risk0"));
                risk1AQList.add(jsonObjectaq.getDoubleValue("risk1"));
                risk2AQList.add(jsonObjectaq.getDoubleValue("risk2"));
                risk3AQList.add(jsonObjectaq.getDoubleValue("risk3"));
                risk4AQList.add(jsonObjectaq.getDoubleValue("risk4"));
                firstWeightList.add(jsonObjectaq.getDoubleValue("secondWeight"));
            }
        }
        resultAQList.add(pointsCalculate.getFuzzyResult(firstWeightList, risk0AQList));
        resultAQList.add(pointsCalculate.getFuzzyResult(firstWeightList, risk1AQList));
        resultAQList.add(pointsCalculate.getFuzzyResult(firstWeightList, risk2AQList));
        resultAQList.add(pointsCalculate.getFuzzyResult(firstWeightList, risk3AQList));
        resultAQList.add(pointsCalculate.getFuzzyResult(firstWeightList, risk4AQList));
        normalAQResultList = StringOperate.normalization2(resultAQList);
        for (int j = 0; j < list.size(); j++) {
            List<Double> resultList = new ArrayList<>();
            List<Double> normalResultList = new ArrayList<>();
            List<Double> secondWeightList = new ArrayList<>();
            List<Double> risk1List = new ArrayList<>();
            List<Double> risk2List = new ArrayList<>();
            List<Double> risk3List = new ArrayList<>();
            List<Double> risk4List = new ArrayList<>();
            List<Double> risk0List = new ArrayList<>();
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = JSONObject.parseObject(jsonArray.getString(i));
                if (jsonObject.getString("firstLevelIndicators").equals(list.get(j))) {
                    risk0List.add(jsonObject.getDoubleValue("risk0"));
                    risk1List.add(jsonObject.getDoubleValue("risk1"));
                    risk2List.add(jsonObject.getDoubleValue("risk2"));
                    risk3List.add(jsonObject.getDoubleValue("risk3"));
                    risk4List.add(jsonObject.getDoubleValue("risk4"));
                    secondWeightList.add(jsonObject.getDoubleValue("secondWeight"));
                }
            }
            resultList.add(pointsCalculate.getFuzzyResult(secondWeightList, risk0List));
            resultList.add(pointsCalculate.getFuzzyResult(secondWeightList, risk1List));
            resultList.add(pointsCalculate.getFuzzyResult(secondWeightList, risk2List));
            resultList.add(pointsCalculate.getFuzzyResult(secondWeightList, risk3List));
            resultList.add(pointsCalculate.getFuzzyResult(secondWeightList, risk4List));

            normalResultList = StringOperate.normalization2(resultList);
            //            综合结果修正

            double sum = (double)Math.round(normalResultList.get(0)*1000)/1000+
                    (double)Math.round(normalResultList.get(1)*1000)/1000+
                    (double)Math.round(normalResultList.get(2)*1000)/1000+
                    (double)Math.round(normalResultList.get(3)*1000)/1000+
                    (double)Math.round(normalResultList.get(4)*1000)/1000;
            double deta=1.000-sum;
            if(Math.abs(deta)>0.005){
                log.error("误差为：{} 大于0.005",deta);
            }
            if(deta>0.000){
                for(int index=4;index>=0;index--){
                    if((double)Math.round(normalResultList.get(index)*1000)/1000!=0.000){
                        normalResultList.set(index,(double)Math.round(normalResultList.get(index)*1000)/1000+deta);
                        break;
                    }
                }
            }else{
                for(int index=0;index<5;index++){
                    if((double)Math.round(normalResultList.get(index)*1000)/1000!=0.000){
                        normalResultList.set(index,(double)Math.round(normalResultList.get(index)*1000)/1000+deta);
                        break;
                    }
                }
            }
            log.error("0:{} 1:{} 2:{} 3:{} 4:{} sum:{}",(double)Math.round(normalResultList.get(0)*1000)/1000,(double)Math.round(normalResultList.get(1)*1000)/1000,
                    (double)Math.round(normalResultList.get(2)*1000)/1000,(double)Math.round(normalResultList.get(3)*1000)/1000,
                    (double)Math.round(normalResultList.get(4)*1000)/1000,sum);

            int maxIndex = normalResultList.lastIndexOf(Collections.max(normalResultList));
            String riskResult = "";
            switch (maxIndex) {
                case 0:
                    riskResult = "\"可忽略风险\"";
                    break;
                case 1:
                    riskResult = "\"可容忍风险\"";
                    break;
                case 2:
                    riskResult = "\"可接受风险\"";
                    break;
                case 3:
                    riskResult = "\"不可接受风险\"";
                    break;
                case 4:
                    riskResult = "\"拒绝接受风险\"";
                    break;
                default:
            }
            if (list.get(j).equals("安全实体")) {
                if (fuzzyResultCount == 0) {
                    Fuzzyresult fuzzyresult = new Fuzzyresult();
                    fuzzyresult.setCheckId(checkId);
                    fuzzyresult.setResultType(1);
                    fuzzyresult.setFirstId(1);
                    fuzzyresult.setSecondId(-1);
                    fuzzyresult.setRisk1Point(Double.parseDouble(df.format(normalResultList.get(0))));
                    fuzzyresult.setRisk2Point(Double.parseDouble(df.format(normalResultList.get(1))));
                    fuzzyresult.setRisk3Point(Double.parseDouble(df.format(normalResultList.get(2))));
                    fuzzyresult.setRisk4Point(Double.parseDouble(df.format(normalResultList.get(3))));
                    fuzzyresult.setRisk5Point(Double.parseDouble(df.format(normalResultList.get(4))));
                    fuzzyresultMapper.insert(fuzzyresult);
                }
                allAQResultList.add("该项目" + list.get(j) + "风险等级对评语集{" + "\"可忽略风险\"" + "\"可容忍风险\"" +
                        "\"可接受风险\"" + "\"不可接受风险\"" + "\"拒绝接受风险\"" + "}的隶属度分别为{" + df.format(normalResultList.get(0)) + "," +
                        df.format(normalResultList.get(1)) + "," + df.format(normalResultList.get(2)) + "," + df.format(normalResultList.get(3)) +
                        "," + df.format(normalResultList.get(4)) + "}," + "依据最大隶属度原则,该项目风险状态隶属于" + riskResult);
            } else {
                if (fuzzyResultCount == 0) {
                    Fuzzyresult fuzzyresult = new Fuzzyresult();
                    fuzzyresult.setCheckId(checkId);
                    fuzzyresult.setResultType(2);
                    fuzzyresult.setFirstId(-1);
                    fuzzyresult.setSecondId(fuzzyindicatorsMapper.getNumberByCheckIdAndName(checkId, list.get(j)));
                    fuzzyresult.setRisk1Point(Double.parseDouble(df.format(normalResultList.get(0))));
                    fuzzyresult.setRisk2Point(Double.parseDouble(df.format(normalResultList.get(1))));
                    fuzzyresult.setRisk3Point(Double.parseDouble(df.format(normalResultList.get(2))));
                    fuzzyresult.setRisk4Point(Double.parseDouble(df.format(normalResultList.get(3))));
                    fuzzyresult.setRisk5Point(Double.parseDouble(df.format(normalResultList.get(4))));
                    fuzzyresultMapper.insert(fuzzyresult);
                }
                allOtherResultList.add("该项目" + list.get(j) + "风险等级对评语集{" + "\"可忽略风险\"" + "\"可容忍风险\"" +
                        "\"可接受风险\"" + "\"不可接受风险\"" + "\"拒绝接受风险\"" + "}的隶属度分别为{" + df.format(normalResultList.get(0)) + "," +
                        df.format(normalResultList.get(1)) + "," + df.format(normalResultList.get(2)) + "," + df.format(normalResultList.get(3)) +
                        "," + df.format(normalResultList.get(4)) + "}," + "依据最大隶属度原则,该项目风险状态隶属于" + riskResult);
                List<Double> firstWeightLists = new ArrayList<>();
                List<Double> allOverNormalResultList = new ArrayList<>();
                firstWeightLists.add(Double.parseDouble(fuzzyindicatorsMapper.getFirstWeightByName("安全实体", checkId)));
                firstWeightLists.add(Double.parseDouble(fuzzyindicatorsMapper.getFirstWeightByName(list.get(j), checkId)));
                List<Double> risk1AllList = new ArrayList<>();
                List<Double> risk2AllList = new ArrayList<>();
                List<Double> risk3AllList = new ArrayList<>();
                List<Double> risk4AllList = new ArrayList<>();
                List<Double> risk0AllList = new ArrayList<>();
                risk0AllList.add(normalAQResultList.get(0));
                risk0AllList.add(normalResultList.get(0));
                risk1AllList.add(normalAQResultList.get(1));
                risk1AllList.add(normalResultList.get(1));
                risk2AllList.add(normalAQResultList.get(2));
                risk2AllList.add(normalResultList.get(2));
                risk3AllList.add(normalAQResultList.get(3));
                risk3AllList.add(normalResultList.get(3));
                risk4AllList.add(normalAQResultList.get(4));
                risk4AllList.add(normalResultList.get(4));
                allOverNormalResultList.add(pointsCalculate.getFuzzyResult(firstWeightLists, risk0AllList));
                allOverNormalResultList.add(pointsCalculate.getFuzzyResult(firstWeightLists, risk1AllList));
                allOverNormalResultList.add(pointsCalculate.getFuzzyResult(firstWeightLists, risk2AllList));
                allOverNormalResultList.add(pointsCalculate.getFuzzyResult(firstWeightLists, risk3AllList));
                allOverNormalResultList.add(pointsCalculate.getFuzzyResult(firstWeightLists, risk4AllList));
                allOverNormalResultList = StringOperate.normalization2(allOverNormalResultList);
                //            综合结果修正

                double sum1 = (double)Math.round(allOverNormalResultList.get(0)*1000)/1000+
                        (double)Math.round(allOverNormalResultList.get(1)*1000)/1000+
                        (double)Math.round(allOverNormalResultList.get(2)*1000)/1000+
                        (double)Math.round(allOverNormalResultList.get(3)*1000)/1000+
                        (double)Math.round(allOverNormalResultList.get(4)*1000)/1000;
                double deta1=1.000-sum1;
                if(Math.abs(deta1)>0.005){
                    log.error("误差为：{} 大于0.005",deta1);
                }
                if(deta>0.000){
                    for(int index=4;index>=0;index--){
                        if((double)Math.round(allOverNormalResultList.get(index)*1000)/1000!=0.000){
                            allOverNormalResultList.set(index,(double)Math.round(allOverNormalResultList.get(index)*1000)/1000+deta1);
                            break;
                        }
                    }
                }else{
                    for(int index=0;index<5;index++){
                        if((double)Math.round(allOverNormalResultList.get(index)*1000)/1000!=0.000){
                            allOverNormalResultList.set(index,(double)Math.round(allOverNormalResultList.get(index)*1000)/1000+deta1);
                            break;
                        }
                    }
                }
                log.error("0:{} 1:{} 2:{} 3:{} 4:{} sum:{}",(double)Math.round(allOverNormalResultList.get(0)*1000)/1000,(double)Math.round(allOverNormalResultList.get(1)*1000)/1000,
                        (double)Math.round(allOverNormalResultList.get(2)*1000)/1000,(double)Math.round(allOverNormalResultList.get(3)*1000)/1000,
                        (double)Math.round(allOverNormalResultList.get(4)*1000)/1000,sum);

                int allOverMaxIndex = allOverNormalResultList.lastIndexOf(Collections.max(allOverNormalResultList));
                String alloverRiskResult = "";
                switch (allOverMaxIndex) {
                    case 0:
                        alloverRiskResult = "\"可忽略风险\"";
                        break;
                    case 1:
                        alloverRiskResult = "\"可容忍风险\"";
                        break;
                    case 2:
                        alloverRiskResult = "\"可接受风险\"";
                        break;
                    case 3:
                        alloverRiskResult = "\"不可接受风险\"";
                        break;
                    case 4:
                        alloverRiskResult = "\"特别重大风险\"";
                        break;
                    default:
                }
                if (fuzzyResultCount == 0) {
                    Fuzzyresult fuzzyresult = new Fuzzyresult();
                    fuzzyresult.setCheckId(checkId);
                    fuzzyresult.setResultType(3);
                    fuzzyresult.setFirstId(1);
                    fuzzyresult.setSecondId(fuzzyindicatorsMapper.getNumberByCheckIdAndName(checkId, list.get(j)));
                    fuzzyresult.setRisk1Point(Double.parseDouble(df.format(normalResultList.get(0))));
                    fuzzyresult.setRisk2Point(Double.parseDouble(df.format(normalResultList.get(1))));
                    fuzzyresult.setRisk3Point(Double.parseDouble(df.format(normalResultList.get(2))));
                    fuzzyresult.setRisk4Point(Double.parseDouble(df.format(normalResultList.get(3))));
                    fuzzyresult.setRisk5Point(Double.parseDouble(df.format(normalResultList.get(4))));
                    fuzzyresultMapper.insert(fuzzyresult);
                }
                allOverResultList.add("该项目" + list.get(j).substring(0, list.get(j).length() - 4) + "风险等级对评语集{" + "\"可忽略风险\"" + "\"可容忍风险\"" +
                        "\"可接受风险\"" + "\"不可接受风险\"" + "\"拒绝接受风险\"" + "}的隶属度分别为{" + df.format(allOverNormalResultList.get(0)) + "," +
                        df.format(allOverNormalResultList.get(1)) + "," + df.format(allOverNormalResultList.get(2)) + "," + df.format(allOverNormalResultList.get(3)) +
                        "," + df.format(allOverNormalResultList.get(4)) + "}," + "依据最大隶属度原则,该项目风险状态隶属于" + alloverRiskResult);
            }
        }
        map.put("aq", allAQResultList);
        map.put("other", allOtherResultList);
        map.put("allOver", allOverResultList);
        allResultList.add(map);
        return allResultList;
    }

    @Override
    public String addZj(Specialistinfo zj) {
        specialistinfoMapper.insert(zj);
        return "success";
    }

    @Override
    public String deleteZj(int mkey) {
        specialistinfoMapper.deleteByPrimaryKey(mkey);
        return "success";
    }

    @Override
    public String deleteItem(int id, int specialistId, int id3) {
        judgeinfoMapper.deleteById1(id, specialistId,id3);
        return "success";
    }

    @Override
    public Object getFuzzyResult(String check, int clusterType) {
        JSONArray jsonArray = JSONArray.parseArray(check);
        List<Fuzzyresult> list = new ArrayList<>();
        List<Object> resultList = new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            switch (clusterType) {
                case 1:
                    list.add(fuzzyresultMapper.selectAllInfoByCheckIdType1(jsonArray.getIntValue(i)));
                    break;
                case 2:
                    list.add(fuzzyresultMapper.selectAllInfoByCheckIdType2(jsonArray.getIntValue(i)));
                    break;
                case 3:
                    list.add(fuzzyresultMapper.selectAllInfoByCheckIdType3(jsonArray.getIntValue(i)));
                    break;
                default:
            }

        }
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("projectName", contracttaskprojectMapper.getProjectNameByCheckId(list.get(i).getCheckId()));
            map.put("risk1Point", list.get(i).getRisk1Point());
            map.put("risk2Point", list.get(i).getRisk2Point());
            map.put("risk3Point", list.get(i).getRisk3Point());
            map.put("risk4Point", list.get(i).getRisk4Point());
            map.put("risk5Point", list.get(i).getRisk5Point());
            resultList.add(map);
        }
        return resultList;
    }

    @Override
    public Object getClusterCaculate(String fuzzyResult, int caculateType) {
        DecimalFormat df = new DecimalFormat("0.00");
        JSONArray jsonArray = JSONArray.parseArray(fuzzyResult);
        double[][] m = new double[5][jsonArray.size()];//将拿到的模糊评价信息进行转置，成为矩阵m
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            m[0][i] = jsonObject.getDoubleValue("risk1Point");
            m[1][i] = jsonObject.getDoubleValue("risk2Point");
            m[2][i] = jsonObject.getDoubleValue("risk3Point");
            m[3][i] = jsonObject.getDoubleValue("risk4Point");
            m[4][i] = jsonObject.getDoubleValue("risk5Point");
        }
        double[][] r = new double[jsonArray.size()][jsonArray.size()];//矩阵r为相似矩阵
        if(caculateType==1)//夹角余弦法
        {
            for(int i=0;i<jsonArray.size();i++)
            {
                for(int j=0;j<jsonArray.size();j++)
                {
                    double xik2=0;
                    double xjk2=0;
                    double xikxjk=0;
                    for(int k=0;k<5;k++)
                    {
                        xik2+=m[k][i]*m[k][i];
                        xjk2+=m[k][j]*m[k][j];
                        xikxjk+=m[k][i]*m[k][j];
                    }
                    System.out.println(xikxjk+""+xik2+""+xjk2);
                    r[i][j]=Double.parseDouble(df.format(xikxjk/(Math.pow(xik2,(double)1/2)*Math.pow(xjk2,(double) 1/2))));
                }
            }
        }else if(caculateType==2)//数量积法
        {
            double max=0;
            for(int i=0;i<jsonArray.size();i++)
            {
                for(int j=0;j<jsonArray.size();j++)
                {
                    double xikxjk=0;
                    if(i==j)
                    {
                        r[i][j]=1;
                    }else
                    {
                        for(int k=0;k<5;k++)
                        {
                            xikxjk+=m[k][i]*m[k][j];
                        }
                        r[i][j]=xikxjk;
                        if(r[i][j]>max)
                        {
                            max=r[i][j];
                        }
                    }
                }
            }
            System.out.println(JSON.toJSON(r));
            //求出最终r矩阵
            for(int i=0;i<jsonArray.size();i++)
            {
                for(int j=0;j<jsonArray.size();j++)
                {
                    if(i==j)
                    {
                        r[i][j]=1;
                    }else
                    {
                        r[i][j]=r[i][j]/max;
                    }
                }
            }
        }
        System.out.println(JSON.toJSON(r));
        double[][] r1=StringOperate.clusterCaculate(r,jsonArray.size());
        double[][] r2=StringOperate.clusterCaculate(r1,jsonArray.size());
        double[][] r3=StringOperate.clusterCaculate(r2,jsonArray.size());
        double[][] r4=StringOperate.clusterCaculate(r3,jsonArray.size());
        List<Double> list=new ArrayList<>();
        List<String> concluList=new ArrayList<>();
        for(int i=0;i<jsonArray.size();i++)
        {
            for(int j=0;j<jsonArray.size();j++)
            {
                if(list.indexOf(r4[i][j])==-1)
                {
                    list.add(r4[i][j]);
                }
            }
        }
        Collections.sort(list);
        for(int i=0;i<list.size();i++)
        {
            String conclu="当阈值λ为"+list.get(i)+"时";
            List<List<Integer>> allList=new ArrayList<>();
            for(int j=0;j<jsonArray.size();j++)
            {
                List<Integer> innerList=new ArrayList<>();
                for(int u=0;u<jsonArray.size();u++)
                {
                    if(list.get(i)>r4[u][j])
                    {
                        innerList.add(0);
                    }else {
                        innerList.add(1);
                    }
                }
                allList.add(innerList);
            }
            List<Integer> countList=new ArrayList<>();
            for(int c=0;c<allList.size();c++)
            {
                if(allList.indexOf(allList.get(c))!=allList.lastIndexOf(allList.get(c)))
                {
                    if(countList.indexOf(c)==-1)
                    {
                        for(int p=0;p<allList.size();p++)
                        {
                            if(allList.get(p).equals(allList.get(c)))
                            {
                                JSONObject jsonObject=jsonArray.getJSONObject(p);
                                conclu+=","+jsonObject.getString("projectName");
                                countList.add(p);
                            }
                        }
                        conclu+="为一类";
                    }
                }else {
                    JSONObject jsonObject=jsonArray.getJSONObject(c);
                   conclu+= ","+jsonObject.getString("projectName")+"为一类";
                }
            }
            concluList.add(conclu);
        }
        return concluList;
    }

    @Override
    public List<Object> getItemByDepth(int nodeDepth,int preNum) {
        return newprefixsystemMapper.getItemByDepth(nodeDepth,preNum);
    }

    @Override
    public Object getChooseInfoForJudge1(int preNum, String nodeDepth) {
        List<Object> list = new ArrayList<Object>();
        List<Object> pNname3InfoList = newprefixsystemMapper.getNumberForJudge(nodeDepth, preNum);
        String pName3Info = JSON.toJSONString(pNname3InfoList, SerializerFeature.WriteMapNullValue, SerializerFeature.DisableCircularReferenceDetect);
        JSONArray pNname3InfoArray = JSONArray.parseArray(pName3Info);
        for (int i = 0; i < pNname3InfoArray.size(); i++) {
            List<Object> listName3 = new ArrayList<Object>();
            Map<String, Object> name3Map = new HashMap<String, Object>();
            int thirdId = pNname3InfoArray.getIntValue(i);
            String pName3 = newprefixsystemMapper.getName4ByName4Id(thirdId);
            List<Object> forthIdInfoList = newprefixsystemMapper.getNumberForJudge("4", thirdId);
            String secondIdInfo = JSON.toJSONString(forthIdInfoList, SerializerFeature.WriteMapNullValue, SerializerFeature.DisableCircularReferenceDetect);
            JSONArray fourthIdInfoArray = JSONArray.parseArray(secondIdInfo);
            List<Object> listName4 = new ArrayList<Object>();
            for (int j = 0; j < fourthIdInfoArray.size(); j++) {

                Map<String, Object> name4Map = new HashMap<String, Object>();
                int fourId = fourthIdInfoArray.getIntValue(j);

                String pName4 = newprefixsystemMapper.getName4ByName4Id(fourId);
                name4Map.put("id", fourId);
                name4Map.put("name", pName4);

                listName4.add(name4Map);
            }
            name3Map.put("id", thirdId);
            name3Map.put("name", pName3);
            name3Map.put("children", listName4);
            list.add(name3Map);
        }
        return list;

    }

    @Override
    public Object getAllJudgeInfo1(int specialistId, int id3) {
        List<Object> list = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        List<Object> list1 = new ArrayList<>();
        List<Object> list2 = new ArrayList<>();
        List<Integer> allItemsList = judgeinfoMapper.getAllId1(specialistId,id3);
        for (int i = 0; i < allItemsList.size() + 1; i++) {
            Map<String, Object> map1 = new HashMap<>();
            if (i == 0) {
                map1.put("id", "name");
                map1.put("value", "检查项");
            } else {
                map1.put("id", allItemsList.get(i - 1).toString());
                map1.put("value", newprefixsystemMapper.getName4ByName4Id(allItemsList.get(i - 1)));
            }
            if (i < allItemsList.size()) {
                Map<String, Object> map2 = new HashMap<>();
                map2.put("name", newprefixsystemMapper.getName4ByName4Id(allItemsList.get(i)));
                map2.put("id", allItemsList.get(i));
                for (int j = 0; j < allItemsList.size(); j++) {
                    map2.put(allItemsList.get(j).toString(), judgeinfoMapper.getPointById1AndId21(specialistId, allItemsList.get(j), allItemsList.get(i),id3));
                }
                list2.add(map2);
            }
            list1.add(map1);
        }
        map.put("list1", list1);
        map.put("list2", list2);
        list.add(map);
        return list;
    }

    @Override
    public Object getAllJudgeResult1(int checkId) {
        int countFuzzyIndicators = fuzzyindicatorsMapper.getCountByCheckId(checkId);
        List<Object> list = new ArrayList<>();
        List<Object> list1 = new ArrayList<>();
        List<Object> list2 = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        //看检查的第二级id是否唯一
        List<Integer> allSecondId = problemcollectMapper.getAllSecondIdByCheck(checkId,1);
        if(allSecondId.size()>1){return "error";}
        int id3 = allSecondId.get(0);

        System.out.println("第二级id的长度"+allSecondId.size()+" "+id3);
        List<Integer> allItemsList = problemCollectDetailMapper.getAllFourthIdByNum(checkId, 1);
        List<Specialistinfo> specialistList = specialistinfoMapper.getAllSpecialistInfoById3(id3);
        for (int i = 0; i < allItemsList.size() + 1; i++) {
            Map<String, Object> map1 = new HashMap<>();
            if (i == 0) {
                map1.put("id", "name");
                map1.put("value", "专家姓名");
            } else {
                map1.put("id", allItemsList.get(i - 1).toString());
                map1.put("value", newprefixsystemMapper.getName4ByName4Id(allItemsList.get(i - 1)));
            }
            list1.add(map1);
        }
        List<Float> allWeightList = new ArrayList<>();
        for (int i = 0; i < specialistList.size(); i++) {
            List<Float> weightList = new ArrayList<>();
            for (int k = 0; k < allItemsList.size(); k++) {
                List<Float> everyLineList = new ArrayList<>();
                for (int m = 0; m < allItemsList.size(); m++) {
                    System.out.println(judgeinfoMapper.getEveryLine1(allItemsList.get(m), allItemsList.get(k), specialistList.get(i).getMkey(),id3));
                    everyLineList.add(judgeinfoMapper.getEveryLine1(allItemsList.get(m), allItemsList.get(k), specialistList.get(i).getMkey(),id3));
                }
                weightList.add(StringOperate.multiply(everyLineList));
            }
            List<Float> wiList = StringOperate.normalization(weightList);
            Map<String, Object> map2 = new HashMap<>();
            map2.put("name", specialistList.get(i).getName());
            for (int j = 0; j < allItemsList.size(); j++) {
                map2.put(allItemsList.get(j).toString(), wiList.get(j));
                Float judgeWeight = (float) pointsCalculate.getPercentInstance(specialistList.get(i).getJudgeWeight());
                allWeightList.add(wiList.get(j) * judgeWeight);
            }
            list2.add(map2);
        }
        Map<String, Object> map2 = new HashMap<>();
        List<Float> list3 = new ArrayList<>();
        map2.put("name", "总权重");
        for (int i = 0; i < allItemsList.size(); i++) {
            Float weight = 0f;
            for (int j = i; j < allWeightList.size(); j += allItemsList.size()) {
                weight += allWeightList.get(j);
            }
            map2.put(allItemsList.get(i).toString(), weight);
            list3.add(weight);
        }
        int allGLFourthIdCount=problemCollectDetailMapper.getAllGLFourthCount(checkId);
        if (countFuzzyIndicators == 0) {
            Fuzzyindicators fuzzyindicators = new Fuzzyindicators();
            fuzzyindicators.setCheckId(checkId);
            fuzzyindicators.setNumber(-1);
            fuzzyindicators.setIndicatorsLevel(-1);
            fuzzyindicators.setIndicatorsName("安全实体");
            if(allGLFourthIdCount==0)
            {
                fuzzyindicators.setIndicatorsWeight("1");
            }else {
                fuzzyindicators.setIndicatorsWeight("0.7");
            }
            fuzzyindicatorsMapper.insert(fuzzyindicators);
        }
        List<Integer> insertList = problemCollectDetailMapper.getAllFourthId(checkId);
        for (int i = 0; i < insertList.size(); i++) {
            if (newprefixsystemMapper.getFather(newprefixsystemMapper.getFather(newprefixsystemMapper.getFather(insertList.get(i)).getPreNum()).getPreNum()).getPreNum() == 1) {
                System.out.println("窝窝头，一块钱四个，嘿嘿");
            } else {
                if (countFuzzyIndicators == 0) {
                    int countThirdIdInIndicators = fuzzyindicatorsMapper.getCountByNum(checkId, newprefixsystemMapper.getFather(insertList.get(i)).getPreNum());
                    if (countThirdIdInIndicators == 0) {
                        Fuzzyindicators fuzzyindicators = new Fuzzyindicators();
                        fuzzyindicators.setCheckId(checkId);
                        fuzzyindicators.setIndicatorsLevel(1);
                        fuzzyindicators.setNumber(newprefixsystemMapper.getFather(insertList.get(i)).getPreNum());
                        fuzzyindicators.setIndicatorsName(newprefixsystemMapper.getName4ByName4Id(newprefixsystemMapper.getFather(insertList.get(i)).getPreNum()) + "管理行为");
                        fuzzyindicators.setIndicatorsWeight("0.3");
                        fuzzyindicatorsMapper.insert(fuzzyindicators);
                    } else {
                        System.out.println("窝窝头，四块钱一个，嘿嘿");
                    }
                }
                if (countFuzzyIndicators == 0) {
                    Fuzzyindicators fuzzyindicators = new Fuzzyindicators();
                    fuzzyindicators.setCheckId(checkId);
                    fuzzyindicators.setNumber(insertList.get(i));
                    fuzzyindicators.setIndicatorsLevel(2);
                    fuzzyindicators.setIndicatorsName(newprefixsystemMapper.getName4ByName4Id(insertList.get(i)));
                    if(newprefixsystemMapper.getName4ByName4Id(insertList.get(i)).equals("安全文明"))
                    {
                        fuzzyindicators.setIndicatorsWeight("0.7");
                    }else{
                        fuzzyindicators.setIndicatorsWeight("0.3");
                    }
                    fuzzyindicatorsMapper.insert(fuzzyindicators);
                }
            }
        }
        list2.add(map2);
        List<Fuzzyindicators> list4 = fuzzyindicatorsMapper.selectAllByCheckId(checkId);
        map.put("list1", list1);
        map.put("list2", list2);
        map.put("list3", list3);
        map.put("list4", list4);
        list.add(map);
        return list;
    }

    @Override
    public String AddJudgeInfo1(String judgeInfos) {
        JSONArray list1 =JSON.parseArray(judgeInfos);
        System.out.println("前台数据"+judgeInfos);
        List<Judgeinfo> list = JSONObject.parseArray(list1.toJSONString(), Judgeinfo.class);
//        List<Judgeinfo> result = new ArrayList<Judgeinfo>();
        if(list.size()>0){

            for(int i=0;i<list.size();i++){
                Judgeinfo judgeinfo =list.get(i);
                judgeinfoMapper.addJudgeInfo(judgeinfo);
                System.out.println(list.get(i).getSpecialistId());
//                result.add(list.getInteger(i));
            }
        }
        return "success";
    }

    @Override
    public String addMemberShipInfo1(String memberShipInfos) {
        JSONArray list1 =JSON.parseArray(memberShipInfos);
        System.out.println("前台数据"+memberShipInfos);
        List<Membershipinfo> list = JSONObject.parseArray(list1.toJSONString(), Membershipinfo.class);
//        List<Judgeinfo> result = new ArrayList<Judgeinfo>();
        if(list.size()>0){

            for(int i=0;i<list.size();i++){
                Membershipinfo membershipinfo =list.get(i);
                membershipinfoMapper.addMemberShipInfo(membershipinfo);
//                System.out.println(list.get(i).getSpecialistId());

            }
        }
        return "success";
    }

    @Override
    public String updateIndicatorWeights(String weightData) {
        JSONArray list1 =JSON.parseArray(weightData);
        List<Fuzzyindicators> list = JSONObject.parseArray(list1.toJSONString(), Fuzzyindicators.class);
        if(list.size()>0){
            for(int i=0;i<list.size();i++){
                Fuzzyindicators fuzzyindicators =list.get(i);
                fuzzyindicatorsMapper.updateByPrimaryKeySelective(fuzzyindicators);
            }
        }
        return "success";
    }
}
